package cn.iocoder.yudao.module.promotion.controller.admin.channel;

import cn.iocoder.yudao.module.promotion.controller.admin.channel.vo.*;
import cn.iocoder.yudao.module.promotion.convert.channel.ChannelLinkConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.channel.ChannelLinkDO;
import cn.iocoder.yudao.module.promotion.service.channel.ChannelLinkService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

@Tag(name = "管理后台 - 商城链接配置")
@RestController
@RequestMapping("/promotion/channel-link")
@Validated
public class ChannelLinkController {

    @Resource
    private ChannelLinkService channelLinkService;

    @PostMapping("/create")
    @Operation(summary = "创建商城链接配置")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:create')")
    public CommonResult<Long> createChannelLink(@Valid @RequestBody ChannelLinkCreateReqVO createReqVO) {
        return success(channelLinkService.createChannelLink(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商城链接配置")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:update')")
    public CommonResult<Boolean> updateChannelLink(@Valid @RequestBody ChannelLinkUpdateReqVO updateReqVO) {
        channelLinkService.updateChannelLink(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商城链接配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:delete')")
    public CommonResult<Boolean> deleteChannelLink(@RequestParam("id") Long id) {
        channelLinkService.deleteChannelLink(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商城链接配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:query')")
    public CommonResult<ChannelLinkRespVO> getChannelLink(@RequestParam("id") Long id) {
        ChannelLinkDO channelLink = channelLinkService.getChannelLink(id);
        return success(ChannelLinkConvert.INSTANCE.convert(channelLink));
    }

    @GetMapping("/list")
    @Operation(summary = "获得商城链接配置列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:query')")
    public CommonResult<List<ChannelLinkRespVO>> getChannelLinkList(@RequestParam("ids") Collection<Long> ids) {
        List<ChannelLinkDO> list = channelLinkService.getChannelLinkList(ids);
        return success(ChannelLinkConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商城链接配置分页")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:query')")
    public CommonResult<PageResult<ChannelLinkRespVO>> getChannelLinkPage(@Valid ChannelLinkPageReqVO pageVO) {
        PageResult<ChannelLinkDO> pageResult = channelLinkService.getChannelLinkPage(pageVO);
        return success(ChannelLinkConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商城链接配置 Excel")
    @PreAuthorize("@ss.hasPermission('zfkj:channel-link:export')")
    //@OperateLog(type = EXPORT)
    public void exportChannelLinkExcel(@Valid ChannelLinkExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ChannelLinkDO> list = channelLinkService.getChannelLinkList(exportReqVO);
        // 导出 Excel
        List<ChannelLinkExcelVO> datas = ChannelLinkConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商城链接配置.xls", "数据", ChannelLinkExcelVO.class, datas);
    }

    @PutMapping("/close")
    @Operation(summary = "关闭推广链接")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> closeChannelLink(@RequestParam("id") Long id) {
        channelLinkService.closeChannelLink(id);
        return success(true);
    }
}
